<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Mmeter_read extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');

	}

	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'),array(
	  	'mmeters'	=> 	$this->mmeter_read_model->get_mother_meters()
	  	));
	  $this->load->view('layouts/footer');
	 }

	 public function getprevious(){
		
		$result = $this->mmeter_read_model->get_latest_read($this->input->post('mmeter_id'));
		if($result[0]->last_read_month == date('m') && $result[0]->last_read_year == date('Y')){
			 echo json_encode("false"); 
		}else{
			 echo json_encode($result);
		}
	}


	public function save_read(){

		$data3 = array(
			  ':p_mmeter_id'    	=> $this->input->post('meter_id')
			 ,':p_read_month'     	=> $this->input->post('rm')
			 ,':p_read_year'     	=> $this->input->post('ry')
			 ,':p_previous_read' 	=> $this->input->post('pr')
			 ,':p_prev_read_date'	=> $this->input->post('prd')
			 ,':p_reading'  		=> $this->input->post('cr')
			 ,':p_consumed'      	=> $this->input->post('cons')
			 ,':p_read_date'     	=> $this->input->post('rd')
			 ,':p_user_id'    		=> $this->session->userdata('user_id')
		);
		
		$this->mmeter_read_model->save_reads($data3);


		
		
	}

	public function update_read(){
		$data = array(
			 ':p_mmeter_read_id'	=>	$this->input->post('read_id')
			,':p_reading'			=>	$this->input->post('cr')
			,':p_consumed'			=>	$this->input->post('cons')
			,':p_user_id'			=> 	$this->session->userdata('user_id')
		);

		$this->mmeter_read_model->update_reads($data);
	}


	public function search_mm_read(){

		$query = $this->mmeter_read_model->search_mm_read($this->input->post('s_mm'),$this->input->post('mm_ym'));

		if(!empty($query)) {
			echo json_encode($query);
		}else{
			echo json_encode("none");
		}


	}




		
     












    public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
			'mmeters'	=> 	$this->mmeter_read_model->get_mother_meters()
		));
	}
	public function edit_form($id){

		$query = $this->mmeter_read_model->search_reads_mm($id);
		$this->load->view($this->session->userdata('foredit'),array(
			'mmeters'	=> 	$this->mmeter_read_model->get_mother_meters(),
			'read_data'	=>	$query
		));
	}
	public function view_form($id){
		$query = $this->mmeter_read_model->search_reads_mm($id);
		$this->load->view($this->session->userdata('forview'),array(
			'mmeters'	=> 	$this->mmeter_read_model->get_mother_meters(),
			'read_data'	=>	$query
		));
	}



	

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */